package cn.gov.escience.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gov.escience.model.Project;
import cn.gov.escience.model.User;
import cn.gov.escience.service.ProjectService;
import cn.gov.escience.service.TestService;
import cn.gov.escience.service.UserService;

@Controller
@RequestMapping("/test")
public class TestController {
	private static Logger log = LoggerFactory.getLogger(TestController.class);
	@Autowired
	private TestService testService;

	@Autowired
	private UserService userService;

	@Autowired
	private ProjectService projectService;

	@RequestMapping(value = "/find", method = RequestMethod.GET)
	public String getPassword(@RequestParam("username") String username,
			Model model) {
		log.debug("In viewUser, username={}", username);
		User user = testService.getUserByUsername(username);
		model.addAttribute(user);
		return "home";
	}

	@RequestMapping(value = "/find2/{username}", method = RequestMethod.GET)
	public String getPassword2(@PathVariable("username") String username,
			Map<String, Object> model) {
		User user = testService.getUserByUsername(username);
		model.put("user", user);
		return "home";
	}

	@RequestMapping(value = "/find3", method = RequestMethod.GET)
	public String getPassword3(HttpServletRequest request) {
		String username = request.getParameter("username");
		User user = testService.getUserByUsername(username);
		request.setAttribute("user", user);
		return "home";
	}

	@RequestMapping(value = "/admin", method = RequestMethod.GET, params = "add")
	public String createUser() {
		return "edit";
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(User user) {
		System.out.println(user.getUsername() + "  " + user.getUsername());
		userService.addUser(user);
		// 重定向
		return "redirect:/test/find2/" + user.getUsername();
	}

	@RequestMapping(value = "/testjson", method = RequestMethod.GET)
	public @ResponseBody
	User getUser() {
		return new User(1, "123", "123");
	}

	@RequestMapping(value = "/testjsons", method = RequestMethod.GET)
	public @ResponseBody
	List<User> getUsers() {
		// return userService.getUsers();
		// return userService.getUsersByNameCondition("12");
		return userService.getUserByPassword("1");

		// return null;
	}

	@RequestMapping(value = "/testprojects", method = RequestMethod.GET)
	public @ResponseBody
	List<Project> getProjects() {
		List<Project> projects = projectService.findByAnyword("北京大学");
		// if (projects == null || projects.size() == 0) {
		// return null;
		// }
		// System.out.println(projects.size());
		return projects;
	}
}
